#!/bin/sh
# Not yet working
#
# Run a simple backup  then copy it
#   to another device.
##
TestName="prune-copy-test"
JobName=CopyJobSave
. scripts/functions


scripts/cleanup
scripts/copy-migration-confs
scripts/prepare-disk-changer

# Directory to backup.
# This directory will be created by setup_data().
BackupDirectory="${tmp}/data"

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data data/small.tgz

# the default fileset FS_TESTJOB backups all file and directories defined in "${tmp}/file-list".
echo "${BackupDirectory}" >${tmp}/file-list


sed 's/migrate/copy/g' ${cwd}/bin/susan-dir.conf > ${cwd}/tmp/1
sed 's/Migrate/Copy/g' ${cwd}/tmp/1 > ${cwd}/bin/susan-dir.conf

dircfg=$conf/susan-dir.conf
$bperl -e "add_attribute('$dircfg', 'AutoPrune', 'No', 'Client')"
$bperl -e "add_attribute('$dircfg', 'Job Retention', '1s', 'Client')"

change_jobname NightlySave $JobName
start_test

#
# Note, we first backup into Pool Default,
#          then Copy into Pool Full.
#              Pool Default uses Storage=File
#              Pool Full    uses Storage=DiskChanger

# Write out bconsole commands
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File volume=FileVolume001 Pool=Default
label storage=DiskChanger volume=ChangerVolume001 slot=1 Pool=Full drive=0
label storage=DiskChanger volume=ChangerVolume002 slot=2 Pool=Full drive=0
@# run two jobs (both will be copied)
run job=$JobName level=Full yes
@sleep 5
run job=$JobName level=Full yes
wait
list jobs
list volumes
@#setdebug level=100 dir
@# should copy two jobs
@#setdebug level=51 storage=DiskChanger
@sleep 2
run job=copy-job yes
wait
messages
@#purge volume=FileVolume001
list jobs
list volumes
wait
messages
@#
@# Now do another backup, but level Incremental
@#
@sleep 2
run job=$JobName level=Incremental yes
wait
messages
@#
@# This final job that runs should be Incremental and
@# not upgraded to full.
@sleep 2
@exec "touch $BackupDirectory/fr.po"
run job=$JobName level=Incremental yes
wait
messages
@$out ${cwd}/tmp/log10.out
@sleep 1
setdebug level=1 director
list jobs
sql
SELECT JobId, Name, JobTDate, StartTime, Type, Level, JobFiles, JobStatus
FROM Job ORDER BY StartTime;

prune jobs jobtype=backup yes
list jobs
@################################################################
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
list volumes
restore where=${cwd}/tmp/susan-restores select
unmark *
mark *
done
yes
list volumes
wait
messages
quit
END_OF_DATA

run_susan
check_for_zombie_jobs storage=File
stop_susan

check_two_logs
check_restore_diff

# Now we will verify that the pruning code is working as expected.  Each time,
# we run 'list jobs', 'prune', 'list jobs'. check_prune_list ensures that jobids
# in argument are present in the first 'list jobs', the 'prune' command deletes
# them (same number), and checks that the last 'list jobs' doesn't contain them
# anymore. See scripts/functions.pm for details.

# Copy jobs are pruned like normal jobs after the Job Retention period
# the admin 'c'opy job should also be pruned
# F F c c C C I0 I -> F I
$bperl -e "check_prune_list('$tmp/log10.out',1,5,7,8)"
estat=$(($estat + $?))

end_test
